﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZGEDrySaltery.DbHelper;
using ZGEDrySaltery.Model;

namespace ZGEDrySaltery.DAL
{
    public class ObjectCategoryDAL
    {
        #region 单例模式
        private static ObjectCategoryDAL instance;

        public static ObjectCategoryDAL GetInstance()
        {
            if (instance == null)
            {
                lock (typeof(ObjectCategoryDAL))
                {
                    if (instance == null)
                    {
                        instance = new ObjectCategoryDAL();
                    }
                }
            }
            return instance;
        }
        private ObjectCategoryDAL()
        {
        }

        #endregion

        #region 获取分类列表
        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <param name="starindex">开始下标</param>
        /// <param name="pagesize">分页大小</param>
        /// <param name="title">分类名称</param>
        /// <returns></returns>
        public List<ObjectCategoryModel> GetObjectCategoryList(int starindex, int pagesize, string name)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(@" SELECT a.*,b.`NAME` from offer a
                                   LEFT JOIN object_category b on a.PCID = b.PCID where 1=1");

            if (!string.IsNullOrEmpty(name))
            {
                strsql.AppendFormat(" and NAME ={0}", name);
            }
            strsql.AppendFormat(" order by sort desc");

            if (starindex != 0)
            {
                strsql.AppendFormat(" limit {0},{1}", starindex, pagesize);
            }

            ObjectCategoryModel unit = new ObjectCategoryModel();
            List<ObjectCategoryModel> list = new List<ObjectCategoryModel>();
            try
            {
                using (IDataReader reader = DbHelperMySQL.ExecuteReader(strsql.ToString()))
                {
                    while (reader.Read())
                    {
                        unit = ObjectCategoryToModel(reader);
                        list.Add(unit);
                    }
                }
            }
            catch
            {
                throw;
            }
            return list;
        }

        /// <summary>
        /// 转实体
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public ObjectCategoryModel ObjectCategoryToModel(IDataReader row)
        {
            ObjectCategoryModel model = new ObjectCategoryModel();
            if (row != null)
            {
                if (row["PCID"] != null && row["PCID"].ToString() != "")
                {
                    model.PCID = int.Parse(row["PCID"].ToString());
                }
                if (row["NAME"] != null)
                {
                    model.NAME = row["NAME"].ToString();
                }
                if (row["PINYIN"] != null)
                {
                    model.PINYIN = row["PINYIN"].ToString();
                }
                if (row["CODE"] != null)
                {
                    model.CODE = row["CODE"].ToString();
                }
                if (row["ALIAS"] != null)
                {
                    model.ALIAS = row["ALIAS"].ToString();
                }
                if (row["PREPCID"] != null && row["PREPCID"].ToString() != "")
                {
                    model.PREPCID = int.Parse(row["PREPCID"].ToString());
                }
                if (row["SORT"] != null && row["SORT"].ToString() != "")
                {
                    model.SORT = int.Parse(row["SORT"].ToString());
                }
                if (row["INSERTTIME"] != null && row["INSERTTIME"].ToString() != "")
                {
                    model.INSERTTIME = DateTime.Parse(row["INSERTTIME"].ToString());
                }
                if (row["MODIFYTIME"] != null && row["MODIFYTIME"].ToString() != "")
                {
                    model.MODIFYTIME = DateTime.Parse(row["MODIFYTIME"].ToString());
                }
            }

            return model;
        }
        #endregion 获取商品列表

        #region 获取商品总数
        /// <summary>
        /// 分页获取获取商品列表
        /// </summary>
        /// <param name="pcid">分类ID</param>
        /// <param name="status">状态</param>
        /// <param name="title">商品标题</param>
        /// <returns></returns>
        public int GetOfferCount(int pcid, int status, string title)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(@" SELECT count(0) from Offer where 1=1");
            if (pcid != 0)
            {
                strsql.AppendFormat(" and PCID = {0}", pcid);
            }
            if (status != -1)
            {
                strsql.AppendFormat(" and STATE = {0}", status);
            }
            if (!string.IsNullOrEmpty(title))
            {
                strsql.AppendFormat(" and TITLE ={0}", title);
            }

            try
            {
                object obj = DbHelperMySQL.GetSingle(strsql.ToString());
                if (obj == null)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(obj);
                }
            }
            catch
            {
                throw;
            }
        }
        #endregion
    }
}
